package cn.luoshaocheng.org.controller;

import cn.luoshaocheng.basic.exception.BusinessException;
import cn.luoshaocheng.basic.util.ExcelUtils;
import cn.luoshaocheng.basic.util.PageList;
import cn.luoshaocheng.basic.util.jsonResult;
import cn.luoshaocheng.org.domain.Shop;
import cn.luoshaocheng.org.domain.ShopLog;
import cn.luoshaocheng.org.dto.ShopDto;
import cn.luoshaocheng.org.query.ShopQuery;
import cn.luoshaocheng.org.service.IShopService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService ShopService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    public List<Shop> loadAll() {
        return ShopService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Shop loadById(@PathVariable("id") Long id) {
        return ShopService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public jsonResult deleteById(@PathVariable("id") Long id) {
        try {
            ShopService.delete(id);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param Shop
     * @return
     */
    @PutMapping
    public jsonResult addAndUpdate(@RequestBody Shop Shop) {
        try {
            if (Shop.getId() == null) {
                ShopService.save(Shop);
            } else {
                ShopService.update(Shop);
            }
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param ShopQuery
     * @return
     */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery ShopQuery) {
        return ShopService.queryPage(ShopQuery);

    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    public jsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            ShopService.patchDel(ids);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("删除失败");
        }
    }

    @ApiOperation(value = "商家入驻",notes = "")
    @PostMapping("/settlement")
    public jsonResult settlement(@RequestBody Shop shop) {
        try {
            ShopService.settlement(shop);
            return new jsonResult();
        } catch (BusinessException e) {
            return new jsonResult();
        }
        catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("操作失败，请稍候再试");
        }
    }

    //审核通过
    @PostMapping("/audit/pass")
    public jsonResult pass(@RequestBody ShopLog log){
        try {
            ShopService.pass(log);
            return new jsonResult();
        }catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("审核失败");
        }
    }
    //审核通过
    @PostMapping("/audit/reject")
    public jsonResult reject(@RequestBody ShopLog log){
        try {
            ShopService.reject(log);
            return new jsonResult();
        }catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("驳回");
        }
    }
    @GetMapping("/export")
    public void exportEx(HttpServletResponse response){
        try {
            //查询所有数据
            List<Shop> list = ShopService.loadAll();
            //调用工具类
            ExcelUtils.exportExcel(list,null,"店铺数据",Shop.class,"aa.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart(value = "file",required = true)MultipartFile file){
        try {
            List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
            shops.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/echarts")
    public List<ShopDto> echartsData(){
        List<ShopDto> shopDtos = ShopService.getCountByState();
        return shopDtos;
    }
    }



